package com.fengjue.service.impl;
import java.time.LocalDateTime;

import com.fengjue.common.RespBean;
import com.fengjue.common.RespPageBean;
import com.fengjue.mapper.MailLogMapper;
import com.fengjue.pojo.Employee;
import com.fengjue.mapper.EmployeeMapper;
import com.fengjue.pojo.MailConstants;
import com.fengjue.pojo.MailLog;
import com.fengjue.service.IEmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author fengjue
 * @since 2021-03-10
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    private MailLogMapper mailLogMapper;

    @Override
    public RespBean addEmp(Employee employee) {

        // 处理合同期限，保留2位小数
        LocalDate beginContract = employee.getBeginContract();
        LocalDate endContract = employee.getEndContract();
        // 相差多少天
        long days = beginContract.until(endContract, ChronoUnit.DAYS);
        DecimalFormat decimalFormat = new DecimalFormat("##.00");
        employee.setContractTerm(Double.parseDouble(decimalFormat.format(days / 365.00)));

        if (1 == employeeMapper.insert(employee)) {
            Employee employee1 = employeeMapper.getEmployee(employee.getId()).get(0);
            // 数据库记录发送的消息的主键id
            String msgId = UUID.randomUUID().toString();
            MailLog mailLog = new MailLog();
            mailLog.setMsgId(msgId);
            mailLog.setEid(employee1.getId());
            mailLog.setStatus(0);
            mailLog.setRouteKey(MailConstants.MAIL_ROUTING_KEY_NAME);
            mailLog.setExchange(MailConstants.MAIL_EXCHANGE_NAME);
            mailLog.setCount(0);
            mailLog.setTryTime(LocalDateTime.now().plusMinutes(MailConstants.MSG_TIMEOUT));
            mailLog.setCreateTime(LocalDateTime.now());
            mailLog.setUpdateTime(LocalDateTime.now());

            mailLogMapper.insert(mailLog);

            // 发送信息
            rabbitTemplate.convertAndSend(
                    // 交换机名字
                    MailConstants.MAIL_EXCHANGE_NAME,
                    // 路由key
                    MailConstants.MAIL_ROUTING_KEY_NAME,
                    // 传送的数据
                    employee1,
                    // DB里的落库表里的id,唯一UUID，此处应该用redis优化
                    new CorrelationData(msgId));

            return RespBean.success("员工添加成功");
        }
        return RespBean.error("员工添加失败");


    }

    @Override
    public RespPageBean getEmployeeWithSalary(Integer currentPage, Integer size) {

        Page<Employee> objects = PageHelper.startPage(currentPage, size);
        objects.setCount(false);

        List<Employee> employee = employeeMapper.getEmployeeWithSalary();
        RespPageBean respPageBean = new RespPageBean();
        respPageBean.setData(employee);
        respPageBean.setTotal(300L);


        return respPageBean;
    }
}
